library(tidyr)
library(ggplot2)
library(multiplot)

setwd("E:/5hmc_file/2_5hmc_yjp_bam/ASM/20210120.H3k.analysis/")
file1=read.csv("117012.analysis/result/117K.ASH.promoter.statis.csv")
file1=file1[file1$H3k.group=="H3K9me3",][,1:3]

file2=read.csv("807.analysis/807.ASH.promoter.statis.csv")
file2=file2[file2$H3k.group=="H3K9me3",][,1:3]

names(file1)=c("H3k.group","same","opposite")
names(file2)=c("H3k.group","same","opposite")

file1$num=1
file=gather(file1,key = direction.group,value = counts,same,opposite)
p1=ggplot(file,aes(x=num,y=counts,,group=direction.group))+geom_bar(stat ="identity",width = 0.5,position = "dodge",aes(fill=direction.group))+
  scale_fill_manual(values = alpha(c("#3C5488","#E64B35"),1))+scale_x_continuous(breaks = file$num,labels = file$H3k.group)+	
  geom_text(aes(label = counts),position=position_dodge(width = 1),size = 5,vjust = -0.25)+coord_flip()+theme_classic(base_size = 15)+labs(x="",y="counts",title = "Promoter 117012 ASH")#+guides(fill=F)

file2$num=1
file=gather(file2,key = direction.group,value = counts,same,opposite)
p2=ggplot(file,aes(x=num,y=counts,,group=direction.group))+geom_bar(stat ="identity",width = 0.5,position = "dodge",aes(fill=direction.group))+
  scale_fill_manual(values = alpha(c("#3C5488","#E64B35"),1))+scale_x_continuous(breaks = file$num,labels = file$H3k.group)+	
  geom_text(aes(label = counts),position=position_dodge(width = 1),size = 5,vjust = -0.25)+coord_flip()+theme_classic(base_size = 15)+labs(x="",y="counts",title = "Promoter 807 psy-ASH")#+guides(fill=F)

layout <- matrix(c(1, 2), nrow = 2)
multiplot(plotlist=list(p1,p2),layout=layout)

###
file1=read.csv("117012.analysis/result/117K.ASH..statis.csv")
file1=file1[file1$H3k.group=="H3K27ac",][,1:3]

file2=read.csv("117012.analysis/result.ASM/117K.ASH.overlap.ASM..statis.csv")
file2=file2[file2$H3k.group=="H3K27ac",][,1:3]

names(file1)=c("H3k.group","same","opposite")
names(file2)=c("H3k.group","same","opposite")

file1$num=1
file=gather(file1,key = direction.group,value = counts,same,opposite)
p1=ggplot(file,aes(x=num,y=counts,,group=direction.group))+geom_bar(stat ="identity",width = 0.5,position = "dodge",aes(fill=direction.group))+
  scale_fill_manual(values = alpha(c("#3C5488","#E64B35"),1))+scale_x_continuous(breaks = file$num,labels = file$H3k.group)+	
  geom_text(aes(label = counts),position=position_dodge(width = 1),size = 5,vjust = -0.25)+coord_flip()+theme_classic(base_size = 15)+labs(x="",y="counts",title = "Not Promoter or enhancer 117012 ASH")#+guides(fill=F)

file2$num=1
file=gather(file2,key = direction.group,value = counts,same,opposite)
p2=ggplot(file,aes(x=num,y=counts,,group=direction.group))+geom_bar(stat ="identity",width = 0.5,position = "dodge",aes(fill=direction.group))+
  scale_fill_manual(values = alpha(c("#3C5488","#E64B35"),1))+scale_x_continuous(breaks = file$num,labels = file$H3k.group)+	
  geom_text(aes(label = counts),position=position_dodge(width = 1),size = 5,vjust = -0.25)+coord_flip()+theme_classic(base_size = 15)+labs(x="",y="counts",title = "Not Promoter or enhancer ASM")#+guides(fill=F)

layout <- matrix(c(1, 2), nrow = 2)
multiplot(plotlist=list(p1,p2),layout=layout)

###

file1=read.csv("117012.analysis/result/117K.ASH..statis.csv")
file1=file1[file1$H3k.group=="H3K4me1",][,1:3]

file2=read.csv("117012.analysis/result.ASM/117K.ASH.overlap.ASM..statis.csv")
file2=file2[file2$H3k.group=="H3K4me1",][,1:3]

names(file1)=c("H3k.group","same","opposite")
names(file2)=c("H3k.group","same","opposite")

file1$num=1
file=gather(file1,key = direction.group,value = counts,same,opposite)
p1=ggplot(file,aes(x=num,y=counts,,group=direction.group))+geom_bar(stat ="identity",width = 0.5,position = "dodge",aes(fill=direction.group))+
  scale_fill_manual(values = alpha(c("#3C5488","#E64B35"),1))+scale_x_continuous(breaks = file$num,labels = file$H3k.group)+	
  geom_text(aes(label = counts),position=position_dodge(width = 1),size = 5,vjust = -0.25)+coord_flip()+theme_classic(base_size = 15)+labs(x="",y="counts",title = "Not Promoter or enhancer 117012 ASH")#+guides(fill=F)

file2$num=1
file=gather(file2,key = direction.group,value = counts,same,opposite)
p2=ggplot(file,aes(x=num,y=counts,,group=direction.group))+geom_bar(stat ="identity",width = 0.5,position = "dodge",aes(fill=direction.group))+
  scale_fill_manual(values = alpha(c("#3C5488","#E64B35"),1))+scale_x_continuous(breaks = file$num,labels = file$H3k.group)+	
  geom_text(aes(label = counts),position=position_dodge(width = 1),size = 5,vjust = -0.25)+coord_flip()+theme_classic(base_size = 15)+labs(x="",y="counts",title = "Not Promoter or enhancer ASM")#+guides(fill=F)

layout <- matrix(c(1, 2), nrow = 2)
multiplot(plotlist=list(p1,p2),layout=layout)
